package com.atguigu.gulimail.product.service.impl;

import com.atguigu.gulimail.product.entity.SkuImagesEntity;
import com.atguigu.gulimail.product.entity.SpuInfoDescEntity;
import com.atguigu.gulimail.product.entity.SpuInfoEntity;
import com.atguigu.gulimail.product.service.*;
import com.atguigu.gulimail.product.web.Vo.SkuItemVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.SkuInfoDao;
import com.atguigu.gulimail.product.entity.SkuInfoEntity;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfo) {
        this.save(skuInfo);
    }

    @Override
    public PageUtils queryPageByCondion(Map<String, Object> params) {

        LambdaQueryWrapper<SkuInfoEntity> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty((String)params.get("key")),SkuInfoEntity::getSkuName,params.get("key"));

        String catelogId=(String) params.get("catelogId");
        if(StringUtils.isNotEmpty(catelogId)){
            wrapper.eq(!"0".equalsIgnoreCase(catelogId),SkuInfoEntity::getCatalogId,catelogId);
        }
        String brandId=(String) params.get("brandId");
        if(StringUtils.isNotEmpty(brandId)){
            wrapper.eq(!"0".equalsIgnoreCase(brandId),SkuInfoEntity::getBrandId,brandId);
        }

        String min=(String) params.get("min");
        if(StringUtils.isNotEmpty(min)){
            BigDecimal minDecimal= new BigDecimal(min);

            wrapper.ge(minDecimal.compareTo(new BigDecimal("0"))==1,SkuInfoEntity::getPrice,min);

        }
        String max=(String) params.get("max");

        if(StringUtils.isNotEmpty(max)){
            BigDecimal maxDecimal= new BigDecimal(max);
            wrapper.le(maxDecimal.compareTo(new BigDecimal("0"))==1,SkuInfoEntity::getPrice,params.get("max"));

        }


        IPage<SkuInfoEntity> page=this.page(
                new Query<SkuInfoEntity>().getPage(params),wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getBySpuId(Long spuId) {
        LambdaQueryWrapper<SkuInfoEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SkuInfoEntity::getSpuId,spuId);
        List<SkuInfoEntity> list = this.list(wrapper);
        return list;
    }

    @Autowired
    SpuInfoService spuInfoService;

    @Autowired
    SkuImagesService skuImagesService;

    @Autowired
    SpuInfoDescService spuInfoDescService;
    @Autowired
    AttrGroupService attrGroupService;
    @Autowired
    SkuSaleAttrValueService skuSaleAttrValueService;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;//线程池

    @Override
    public SkuItemVo item(Long skuId) {

        SkuItemVo skuItemVo=new SkuItemVo();

        CompletableFuture<Void> imgAsync = CompletableFuture.runAsync(() -> {
            //根据skuId获取sku的图片
            List<SkuImagesEntity> imageList = skuImagesService.getBySkuId(skuId);
            if (imageList != null) {
                skuItemVo.setImages(imageList);
            }
        }, threadPoolExecutor);



        CompletableFuture<SkuInfoEntity> infoFuture =CompletableFuture.supplyAsync(()->{
            //查询sku基本信息
            SkuInfoEntity info = getById(skuId);
            if(info!=null){
                skuItemVo.setInfo(info);
            }
            return info;
        },threadPoolExecutor);


        CompletableFuture<Void> spuDescAsync =
                infoFuture
                        .thenAcceptAsync((info) -> {
            //查询spu的介绍
            SpuInfoDescEntity spuDesc = spuInfoDescService.getById(info.getSpuId());
            if (spuDesc != null) {
                skuItemVo.setDesc(spuDesc);
            }
        }, threadPoolExecutor);


        CompletableFuture<Void> attrGroupAsync =
                infoFuture
                        .thenAcceptAsync((info) -> {
            //获取spu的规格参数信息
            List<SkuItemVo.SpuItemAttrGroupVo> spuItemAttrGroupVos = attrGroupService.getAttrGroupAndAttrsBySpuId(info.getSpuId(), info.getCatalogId());
            if (spuItemAttrGroupVos != null) {
                skuItemVo.setGroupAttrs(spuItemAttrGroupVos);
            }
        }, threadPoolExecutor);


        CompletableFuture<Void> attrValueAsync =
                infoFuture
                        .thenAcceptAsync(info -> {
            //获取spu的销售属性组合
            //根据SpuId获取sku所有销售属性的组合
            List<SkuItemVo.SkuItemSaleAttrVo> attrGroupVos = skuSaleAttrValueService.getSaleAttrBySpuId(info.getSpuId());
            skuItemVo.setSaleAttr(attrGroupVos);
        }, threadPoolExecutor);


        //等待所有任务都完成
        try {
            CompletableFuture.allOf(imgAsync,attrValueAsync,attrGroupAsync,spuDescAsync).get();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }

        return skuItemVo;
    }

}